package com.bio.jsf;

import com.bio.jpa.entities.Session;
import com.bio.jpa.entities.SessionPK;
import com.bio.jsf.util.JsfUtil;
import com.bio.jpa.session.SessionFacade;

import java.io.Serializable;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.SelectItem;

@ManagedBean(name = "sessionController")
@ApplicationScoped
public class SessionController implements Serializable {

    private Session current;

    public void setCurrent(Session current) {
        this.current = current;
    }
    @EJB
    private SessionFacade ejbFacade;

    public SessionController() {
    }

    public Session getCurrent() {
        if (current == null) {
            current = new Session();
            current.setSessionPK(new SessionPK());
        }
        return current;
    }

    private SessionFacade getFacade() {
        return ejbFacade;
    }

    public String prepareList() {
        return "List";
    }

    public String prepareView() {
        return "View";
    }

    public String prepareCreate() {
        current = new Session();
        current.setSessionPK(new com.bio.jpa.entities.SessionPK());
        return "Create";
    }

    public String create() {
        try {

            System.out.println(current.getSessionPK().getUidFk());
            ejbFacade = new SessionFacade();
            getFacade().create(current);
            System.out.println(getFacade());
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("SessionCreated"));
            return prepareCreate();
        } catch (Exception e) {
            e.printStackTrace();
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String prepareEdit() {
        return "Edit";
    }

    public String update() {
        try {
            getFacade().edit(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("SessionUpdated"));
            return "View";
        } catch (Exception e) {
            e.printStackTrace();
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String destroy() {
        performDestroy();
        return "";
    }

    public String destroyAndView() {
        performDestroy();
        return "View";
    }

    private void performDestroy() {
        try {
            getFacade().remove(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("SessionDeleted"));
        } catch (Exception e) {
            e.printStackTrace();
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
    }

    @FacesConverter(forClass = Session.class)
    public static class SessionControllerConverter implements Converter {

        private static final String SEPARATOR = "#";
        private static final String SEPARATOR_ESCAPED = "\\#";

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            SessionController controller = (SessionController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "sessionController");
            return controller.ejbFacade.find(getKey(value));
        }

        com.bio.jpa.entities.SessionPK getKey(String value) {
            com.bio.jpa.entities.SessionPK key;
            String values[] = value.split(SEPARATOR_ESCAPED);
            key = new com.bio.jpa.entities.SessionPK();
            key.setSesId(Integer.parseInt(values[0]));
            key.setUidFk(Integer.parseInt(values[1]));
            return key;
        }

        String getStringKey(com.bio.jpa.entities.SessionPK value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value.getSesId());
            sb.append(SEPARATOR);
            sb.append(value.getUidFk());
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Session) {
                Session o = (Session) object;
                return getStringKey(o.getSessionPK());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + SessionController.class.getName());
            }
        }
    }
}
